package com.ajie.bluetoothcard.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

/**
 * Table实体 用户创建table
 * @author BigMao
 *
 */
public class Table {
	public static final String Card = "card";
	public static final String Card_Type = "card_type";
	
	private List<Column> columnList = new ArrayList<Column>();
	private String name;
	private String COLUMN_DEFINITION_SEPERATOR = ", ";
	private Context context;
	private DatabaseHelper databaseHelper;
	
	public Table(String name){
		this.name = name;
	}
	
	public Table(String name, DatabaseHelper databaseHelper, Context context) {
		this.name = name;
		this.databaseHelper = databaseHelper;
		if (databaseHelper != null) {
			databaseHelper.addTable(this);
		}
		this.context = context;
	}
	
	public String getCreateString() {
        String sql = "create table " + name;
        if (columnList.size() > 0) {
            sql += "( ";
            for (Column column : columnList) {
                sql += column.getCreateString();
                sql += COLUMN_DEFINITION_SEPERATOR;
            }

            sql = removeEnd(sql, COLUMN_DEFINITION_SEPERATOR);
            sql += " )";
        }
        return sql;
    }
	

	public static String removeEnd(String src, String suffix) {
		if (src.endsWith(suffix)) {
			src = src.substring(0, src.length() - suffix.length());
		}
		return src;
	}
	
	public void beforeCreate(SQLiteDatabase db) {
    
	}

    public void afterCreate(SQLiteDatabase db) {
    
    }
    
    public Table addColumn(Column column) {
        columnList.add(column);
        return this;
    }

    public String getName() {
        return name;
    }
    
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        return databaseHelper.getReadableDatabase().query(getName(), projection, selection, selectionArgs, null, null, sortOrder);
    }
	
    
    public Uri insert(Uri uri, ContentValues values) {
        long id = databaseHelper.getWritableDatabase().insertOrThrow(getName(), "ID", values);
        context.getContentResolver().notifyChange(uri, null);
        return uri.buildUpon().appendPath(String.valueOf(id)).build();
    }
    
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int numDeleted = databaseHelper.getWritableDatabase().delete(getName(), selection, selectionArgs);
        context.getContentResolver().notifyChange(uri, null);
        return numDeleted;
    }

    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        int numUpdated = databaseHelper.getWritableDatabase().update(getName(), values, selection, selectionArgs);
        context.getContentResolver().notifyChange(uri, null);
        return numUpdated;
    }
}
